<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/10 0010
 * Time: 15:00
 */

namespace App\Models\Admin;


class Withdraw extends Common
{
    /**
     * 提现记录表
     * @var string
     */
    protected $table = 'o2oadmin_withdraw';

    /**
     * 获取已提现金额总和
     */
    public function getCashed($node_id, $source_type)
    {
        $res = $this->where('nodeid', $node_id)
            ->where('apply_status', '<', 4)//去除打款失败
            ->where('source_type', $source_type)
            ->sum('apply_amount');
        return floatval($res);
    }

    /**
     * 获取提现记录列表
     *
     * @param $node_id
     * @param $source_type
     * @param array $where
     * @return mixed
     */
    public function getLists($node_id, $source_type, $where = [])
    {
        if (empty($where['sort'])) {
            $sort = 'asc';
        } else {
            $sort = $where['sort'];
        }
        if (empty($where['pagesize'])) {
            $pageSize = 10;
        } else {
            $pageSize = $where['pagesize'];
        }
        return $this->where('nodeid', $node_id)
            ->where('source_type', $source_type)
            ->orderBy('id', $sort)
            ->paginate($pageSize);
    }

    /**
     * 格式化成数组
     *
     * @param $result
     * @return array
     */
    public static function parseForExcel($result)
    {
        $status_arr = [
            1 => '处理中',
            2 => '打款超过',
            3 => '打款失败'
        ];
        $data = [];
        foreach ($result as $key => $item) {
            $data[] = [
                $key + 1,
                $status_arr[$item->apply_status],
                $item->apply_amount,
                $item->poundage,
                $item->apply_time
            ];
        }
        return $data;
    }

    /**
     * 获取今天已提现总额
     *
     * @param $node_id
     * @return float
     */
    public function getCashDay($node_id)
    {
        $res = $this->where('nodeid', $node_id)
            ->whereBetween('apply_time', [
                date('Y-m-d', time()) . ' 00:00:00',
                date('Y-m-d H:i:s', time())
            ])
            ->sum('apply_amount');
        return floatval($res);
    }


}